package aryn.framework.display.drawable;

import java.awt.Graphics;
import java.awt.event.MouseEvent;

import aryn.framework.display.Clickable;
import aryn.framework.display.MyCanvas;
import aryn.framework.display.MyMouseInfo;

public abstract class Drawable implements Clickable {
	private static long count;
	protected long id;
	protected int x;
	protected int y;
	protected int priority;
	protected boolean isVisible;
	protected MyCanvas canvas;
	
	public Drawable(int x, int y, Priority priority, MyCanvas canvas) {
		this.x = x;
		this.y = y;
		this.id = count++;
		this.priority = priority.val;
		this.canvas = canvas;
		this.isVisible = true;
	}
	
	public void drawSelf(Graphics g) {
		if (isVisible)
			draw(g);
	}
	public abstract void draw(Graphics g);
	protected abstract boolean isMouseOver(int mouseX, int mouseY);
	
	protected int getClick() {
		MyMouseInfo info = canvas.getMouseInfo();
		if (isMouseOver() && info.getClick() > 0 && info.isClicked() )
			return info.getClick();
		return -1;
	}
	
	protected int getPress() {
		MyMouseInfo info = canvas.getMouseInfo();
		if (isMouseOver() && info.getClick() > 0 && !info.isClicked() )
			return info.getClick();
		return -1;
	}
	
	@Override
	public boolean isMouseOver() {
		MyMouseInfo info = canvas.getMouseInfo();
		return isMouseOver(info.getMouseX(), info.getMouseY());
	}

	@Override
	public boolean isLeftClicked() {
		return getClick() == MouseEvent.BUTTON1;
	}	

	@Override
	public boolean isRightClicked() {
		return getClick() == MouseEvent.BUTTON2;
	}
	
	@Override
	public boolean isLeftPressed() {
		return getPress() == MouseEvent.BUTTON1;
	}
	
	@Override
	public boolean isRightPressed() {
		return getPress() == MouseEvent.BUTTON2;
	}
	
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public long getId() {
		return id;
	}
	public int getPriority() {
		return priority;
	}
	public void setPriority(int priority) {
		this.priority = priority;
	}
	public boolean isVisible() {
		return isVisible;
	}
	public void setVisible(boolean isVisible) {
		this.isVisible = isVisible;
	}

	public boolean equals(Object obj) {
		if (obj instanceof Drawable) {
			Drawable drawable = (Drawable) obj;
			if (drawable.getId() == this.id)
				return true;
		}
		return false;
	}

	public enum Priority {
		BACKGROUND(0), MIDDLE_BACK(1), MIDDLE(2), MIDDLE_FORE(3), FOREGROUND(4);
		private int val;
		
		private Priority(int val) {
			this.val = val;
		}
		public int getVal() {
			return val;
		}
	}
	
}
